/**
 * 作者:hanbo
 * 日期:2018/12/69:24
 * 描述:两点线几何体
 */
import {Geometry} from "./Geometry";
import {BoundingBox} from "./BoundingBox";
import {Point} from "./Point";
import {LineStyle} from "./style/LineStyle";
import {EXTENDS_3857} from "../core/Constant";

export class Line extends Geometry {
    /**
     * 线构造器
     * @param point1 {Point} 起点
     * @param point2 {Point} 终点
     */
    constructor(point1, point2) {
        super("line", BoundingBox.getPolylineBox(Point.pointsToArray([point1, point2])));
        this.point1 = point1;
        this.point2 = point2;
    }

    /**
     * 设置线样式
     * @param lineStyle {LineStyle} 线样式
     */
    setStyle(lineStyle) {
        super.setStyle(lineStyle)
    }

    getStyle() {
        if (this.style) {
            return this.style
        } else {
            return new LineStyle()
        }
    }

    /**
     * 转到GL绘图坐标系
     */
    toGLCoordinate() {
        if (!this._glPath) {
            this._glPath = [this.point1.x / EXTENDS_3857, this.point1.y / EXTENDS_3857, this.point2.x / EXTENDS_3857, this.point2.y / EXTENDS_3857];
        }
    }

    /**
     * 生成随机线
     * @returns {Line}
     */
    static randomLine() {
        return new Line(Point.randomPoint(), Point.randomPoint())
    }

    /**
     * 根据宽度将线向两侧扩展为矩形
     * @param line {Number[]} 中心线
     * @param width {Number} 线宽
     * @return {Number[][]} 逆时针点坐标1-2-3-4
     */
    static lineToRectangle(line, width) {
        let x1 = line[0];
        let y1 = line[1];
        let x2 = line[2];
        let y2 = line[3];
        let  radian;
            if (x1 === x2) {
            radian=Math.PI/2
        }else {
            let k=(y2 - y1) / (x2 - x1);//求出直线斜率
            radian = Math.atan(k);//夹角弧度值
        }

        let sin = Math.sin(radian) * width/2;
        let cos = Math.cos(radian) * width/2;
        return [[x1 + sin, y1 - cos], [x1 - sin, y1 + cos], [x2 - sin, y2 + cos], [x2 + sin, y2 - cos]]
    }

}

